package com.whiteblue.controller;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;
import com.whiteblue.Interceptor.LoginInterceptor;
import com.whiteblue.model.Book;
import com.whiteblue.model.Category;
import com.whiteblue.model.Post;
import com.whiteblue.model.User;
import com.whiteblue.validator.AddBookValidator;
import com.whiteblue.validator.PostValidator;

import java.util.List;

/**
 * Created by WhiteBlue on 15/4/5.
 */
public class BookController extends Controller {

    public void index() {
        if (getParaToInt("book") == null) {
            this.renderError(404);
        } else {
            int bookId = getParaToInt("book");
            Book book = Book.dao.getById(bookId);
            if (book != null) {
                if (getSessionAttr("user") != null) {
                    User user = getSessionAttr("user");
                    if (user.get("id").equals(book.get("userId"))) {
                        book.readClear();
                    }
                }
                this.setAttr("book", book);
                this.setAttr("page", Post.dao.getPostPage(getParaToInt("page", 1), bookId));
                this.setAttr("actionUrl","/book?book=" + bookId + "&page=");
                this.render("/book_index.html");
            } else {
                this.renderError(404);
            }
        }
    }

    @Before({LoginInterceptor.class, PostValidator.class})
    public void addPost() {
        User user = getSessionAttr("user");
        Post post = getModel(Post.class);
        post.newPost(getParaToInt("book"), user.getInt("id"), getPara("content"));
        this.redirect("/book?book=" + getParaToInt("book")+"&page=1");
    }

    public void listBook() {
        if (getParaToInt("category") == null) {
            this.renderError(404);
        } else {
            int category = getParaToInt("category");
            Page<Book> page = Book.dao.getForCategory(category, getParaToInt("page", 1));
            if (page.getTotalRow() == 0) {
                this.setAttr("none", 1);
            }
            this.setAttr("page", page);
            this.setAttr("category", Category.dao.getById(category));
            this.setAttr("actionUrl", "/book?category=" + category + "&page=");
            this.render("/book_list.html");
        }
    }


    @Before({LoginInterceptor.class, AddBookValidator.class})
    public void addBook() {
        User user = this.getSessionAttr("user");
        Book book = getModel(Book.class);
        if (book.getFromDOUBAN(getPara("ISBN"))) {
            book.addBook(getParaToInt("type"), getParaToInt("category"),
                    getPara("method"), getParaToInt("price", 0), user.getInt("id"), getPara("contact"));
            this.redirect("/book?book=" + book.get("id"));
        } else {
            this.setAttr("msg", "书籍未找到");
            this.render("/info.html");
        }
    }

    public void search() {
        if (getPara("name") == null) {
            this.renderError(404);
        } else {
            String name = getPara("name");
            List<Book> list = Book.dao.findAll(name);
            this.setAttr("books", list);
            this.setAttr("findName", name);
            if (list.isEmpty()) {
                this.setAttr("none", 1);
            }
            this.render("/find_list.html");
        }

    }

    @Before(LoginInterceptor.class)
    public void deleteBook() {
        User user=getSessionAttr("user");
        if(getParaToInt("book")==null){
            this.renderError(404);
        }else{
            Book book=Book.dao.getById(getParaToInt("book"));
            if(book==null){
                this.renderError(404);
            }else{
                book.deleteMe();
                this.redirect("/user/checkBook?user="+user.getInt("id"));
            }
        }
    }
}
